<?php

namespace App\Admin\Components;

use App\Models\Owner;
use Carbon\Carbon;
use Dcat\Admin\Widgets\Metrics\Card;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;

class HouseTotalUsers extends Card
{
    /**
     * 卡片底部内容.
     *
     * @var string|Renderable|\Closure
     */
    protected $footer;

    /**
     * 初始化卡片
     */
    protected function init()
    {
        parent::init();

        $this->title('租客增长表');
        $this->dropdown([
            '7' => '最近七天',
            '28' => '最近28天',
            '30' => '最近30天',
            '365' => '最近一年',
        ]);
    }

    /**
     * 处理请求.
     *
     * @param Request $request
     *
     * @return void
     */
    public function handle(Request $request)
    {
        switch (intval($request->get('option'))) {
            case 365:
                $this->percentage(365);
                break;
            case 30:
                $this->percentage(30);
                break;
            case 28:
                $this->percentage(28);
                break;
            case 7:
                $this->percentage(7);
                break;
            default:
                $this->percentage(7);
        }
    }

    public function percentage($days)
    {
        $addOwnerCount = Owner::query()->where('created_at', '>', Carbon::now()->subDays($days))->count();
        $ownerCount = Owner::query()->count();
        $oldOwnerCount = $ownerCount - $addOwnerCount;
        $this->content($ownerCount);
        if ($oldOwnerCount == 0) {
            $this->up(0);
        } else {
            $num = (int)($addOwnerCount / $oldOwnerCount) * 100;
            if ($num > 0) {
                $this->up($num);
            } else {
                $this->down(abs($num));
            }
        }
    }

    /**
     * @param int $percent
     *
     * @return $this
     */
    public function up($percent)
    {
        return $this->footer(
            "<i class=\"feather icon-trending-up text-success\"></i> {$percent}% 增长"
        );
    }

    /**
     * @param int $percent
     *
     * @return $this
     */
    public function down($percent)
    {
        return $this->footer(
            "<i class=\"feather icon-trending-down text-danger\"></i> {$percent}% 减少"
        );
    }

    /**
     * 设置卡片底部内容.
     *
     * @param string|Renderable|\Closure $footer
     *
     * @return $this
     */
    public function footer($footer)
    {
        $this->footer = $footer;

        return $this;
    }

    /**
     * 渲染卡片内容.
     *
     * @return string
     */
    public function renderContent()
    {
        $content = parent::renderContent();

        return <<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
    <h2 class="ml-1 font-lg-1">{$content}</h2>
</div>
<div class="ml-1 mt-1 font-weight-bold text-80">
    {$this->renderFooter()}
</div>
HTML;
    }

    /**
     * 渲染卡片底部内容.
     *
     * @return string
     */
    public function renderFooter()
    {
        return $this->toString($this->footer);
    }
}
